package com.tarzan.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tarzan.api.model.request.role.*;
import com.tarzan.api.model.response.role.SysRoleInfoResp;
import com.tarzan.api.model.response.role.SysRoleVo;
import com.tarzan.api.model.response.user.SysUserInfoResp;
import com.tarzan.common.core.base.BaseResponse;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.system.service.ISysRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  角色维护
 * </p>
 *
 * @author liujinshan
 * @since 2020-05-19
 */
@RestController
@RequestMapping("role")
public class SysRoleController {
    private final static Logger logger = LoggerFactory.getLogger(SysRoleController.class);

    @Autowired
    private ISysRoleService iSysRoleService;

    /**
     * 根据id获取角色信息
     * @param sysRoleIdReq
     * @return
     */
    @PostMapping(value = "getRoleInfoById",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse<SysRoleInfoResp> getRoleInfoById(@RequestBody SysRoleIdReq sysRoleIdReq){
        BaseResponse<SysRoleInfoResp> baseResponse = new BaseResponse<>();
        try {
            baseResponse = BaseResponse.ok(iSysRoleService.getRoleInfoById(sysRoleIdReq));
        } catch (BusinessException e) {
            baseResponse.setErrCode(e.getCode());
            baseResponse.setErrMsg(e.getMsg());
            logger.info("SysRoleController -> getRoleInfoById() BusinessException ex="+e);
        } catch (Exception e) {
            baseResponse.setErrCode("999");
            baseResponse.setErrMsg(e.getMessage());
            logger.info("SysRoleController -> getRoleInfoById() Exception e="+e);
        }
        return baseResponse;
    }


    /**
     * 分页查询角色信息
     * @param roleInfoReq
     * @return
     */
    @PostMapping(value = "getPageRoleInfo",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse<IPage<SysRoleInfoResp>> getPageRoleInfo(@RequestBody SysRoleInfoReq roleInfoReq){
        BaseResponse<IPage<SysRoleInfoResp>> baseResponse = new BaseResponse<>();
        try {
            baseResponse = BaseResponse.ok(iSysRoleService.getPageRoleInfo(roleInfoReq));
        } catch (BusinessException e) {
            baseResponse.setErrCode(e.getCode());
            baseResponse.setErrMsg(e.getMsg());
            logger.info("SysRoleController -> getPageRoleInfo() BusinessException ex="+e);
        } catch (Exception e) {
            baseResponse.setErrCode("999");
            baseResponse.setErrMsg(e.getMessage());
            logger.info("SysRoleController -> getPageRoleInfo() Exception e="+e);
        }
        return baseResponse;
    }

    /**
     * 新增角色信息
     * @param insertRoleReq
     * @return
     */
    @PostMapping(value = "add",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse add(@RequestBody @Validated InsertRoleReq insertRoleReq, BindingResult bindingResult){
        try {
            iSysRoleService.insert(insertRoleReq);
        } catch (BusinessException e) {
            logger.info("SysRoleController -> update() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e) {
            logger.error("SysRoleController -> update() BusinessException ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /**
     * 修改角色信息
     * @param updateRoleInfoReq
     * @return
     */
    @PostMapping(value = "update",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse update(@RequestBody UpdateRoleInfoReq updateRoleInfoReq){
        try {
            iSysRoleService.upadte(updateRoleInfoReq);
        } catch (BusinessException e) {
            logger.info("SysRoleController -> update() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e) {
            logger.error("SysRoleController -> update() BusinessException ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /**
     * 删除角色信息
     * @param sysRoleIdReq
     * @return
     */
    @PostMapping(value = "delete",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse delete(@RequestBody SysRoleIdReq sysRoleIdReq){
        try {
            iSysRoleService.delete(sysRoleIdReq);
        } catch (BusinessException e) {
            logger.info("SysRoleController -> delete() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e) {
            logger.error("SysRoleController -> delete() BusinessException ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }


    /**
     * 根据角色id查询对应的用户信息
     * @param findUnallocatedUserReq
     * @return
     */
    @PostMapping(value = "findUnallocatedUserByRoleID",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse<IPage<SysUserInfoResp>> findUnallocatedUserByRoleID(@RequestBody FindUnallocatedUserReq findUnallocatedUserReq){
        BaseResponse<IPage<SysUserInfoResp>> baseResponse = new BaseResponse<>();
        try {
            baseResponse = BaseResponse.ok(iSysRoleService.findUnallocatedUserByRoleID(findUnallocatedUserReq));
        } catch (BusinessException e) {
            baseResponse.setErrCode(e.getCode());
            baseResponse.setErrMsg(e.getMsg());
            logger.info("SysRoleController -> getPageUserRoleInfoByRoleId() BusinessException ex="+e);
        } catch (Exception e) {
            baseResponse.setErrCode("999");
            baseResponse.setErrMsg(e.getMessage());
            logger.info("SysRoleController -> getPageUserRoleInfoByRoleId() Exception e="+e);
        }
        return baseResponse;
    }


    /**
     * 查询角色信息不分页
     * @return
     */
    @GetMapping(value = "findAllRoleInfo")
    public BaseResponse<List<SysRoleVo>> findAllRoleInfo(){
        BaseResponse<List<SysRoleVo>> response = new BaseResponse<>();
        try {
            response = BaseResponse.ok(iSysRoleService.findAllRoleInfo());
        } catch (BusinessException e) {
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
            logger.info("SysRoleController -> findAllRoleInfo() BusinessException ex="+e);
        } catch (Exception e) {
            response.setErrCode("999");
            response.setErrMsg(e.getMessage());
            logger.info("SysRoleController -> findAllRoleInfo() Exception e="+e);
        }
        return response;
    }
    
}
